<template>
  <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
    <!-- <van-cell  >
      {{item}}====
     
    </van-cell>-->
    <comment-item
      @replyBtn="$emit('replyBtn',$event)"
      v-for="(item,index) in list"
      :key="index"
      :comment="item"
    ></comment-item>
  </van-list>
</template>
           
<script>
import { getComments_api } from '@/api/comment'

import commentItem from '@/components/comment-item'
export default {
  
  props: {
    artId: {
      type: [String, Number, Object],
      required: true
    },
    newObj: {
      type: [String, Number, Object]
    },
    list: {
      type: [Array, Object],
      default: () => []
    },
    type: {
      type: [String, Number, Object],
      default: 'a'
    }
  },
  components: {
    commentItem
  },
  data() {
    return {
      loading: false,
      finished: false,
      offset: null, // 获取下一页数据的标记
      limit: 10,
      newList: []
    }
  },
  created() {
    // this.getList()
    this.loading = true
    this.onLoad()
  },
  // watch:{
  //   artId(){
  //     this.onLoad()
  //   }
  // },

  methods: {
    // async getList(){
    //   const  {data:res}=await  getComments_api({
    //     type:this.type,
    //     source:this.artId,
    //     offset:this.artId,
    //     limit:10
    //   })
    async onLoad() {
      console.log(this.type, 122222222)
      // 异步更新数据
      const { data: res } = await getComments_api({
        type: this.type, //  评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
        source: this.artId.toString(), // 源id，文章id或评论id
        offset: this.offset ? this.offset.toString() : this.offset, // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
        limit: this.limit // 获取的评论数据个数，不传表示采用后端服务设定的默认每页数据量
      })

      //this.newList = [...this.list,...this.newList ,...res.data.results]
      this.list.push(...res.data.results) //  简单的一行代码
      console.log(this.list, 4455)
      this.$emit('totalCount', res.data.total_count)
      this.loading = false
      if (res.data.results.length) {
        this.offset = res.data.last_id
      } else {
        this.finished = true
      }
      //console.log(res, 555666)
    }
  }
}
</script>
<style scoped>
</style>